package com.example.meetdroid;

import com.example.meetdroid.Interfaces.IPositionEvents;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.meetdroid.main.Globals;
import com.example.meetdroid.main.PositionWatcher;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.meetdroid.R;

public class MapPane extends FragmentActivity implements IPositionEvents{
	  private TextView latituteField;
	  private TextView longitudeField;
	  private PositionWatcher posWatcher;
	  private GoogleMap googleMap;
	  private Button btnFocus;
	/** Called when the activity is first created. */

	  @Override
	  public void onCreate(Bundle savedInstanceState) {
		  try {
		  		super.onCreate(savedInstanceState);
				setContentView(R.layout.main_map_activity);
				//latituteField = (TextView) findViewById(R.id.TextView02);
				//longitudeField = (TextView) findViewById(R.id.TextView04);
				posWatcher = new PositionWatcher(this);
				posWatcher.addListener(this);
				googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
				this.btnFocus = (Button)findViewById(R.id.map_btn_focus);
				
			} catch (Exception e) {
				Log.e("Meetup", e.getMessage().toString());
			}
	  }

	  /* Request updates at startup */
	  @Override
	  protected void onResume() {
	    super.onResume();
	    posWatcher.requestLocationUpdates(400, 1);
	  }

	  /* Remove the locationlistener updates when Activity is paused */
	  @Override
	  protected void onPause() {
	    super.onPause();
	    posWatcher.removeUpdates();
	  }

	  private void setupBtnFocus(){
		  
		  this.btnFocus.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					setFocusToUser();
				}
			});
	  }
	  private void setFocusToUser(){
		  
	  }
		@Override
	public void ProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void ProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void StatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void LocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void LocationChanged(double lat, double lng) {
		LatLng pos = new LatLng(lat, lng);
		googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos,15));
	    // Zoom in, animating the camera.
	    googleMap.animateCamera(CameraUpdateFactory.zoomIn());
	    // Zoom out to zoom level 10, animating with a duration of 2 seconds.
	    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
	    googleMap.clear();
	    
	    googleMap.addMarker(new MarkerOptions().position(pos).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location)).title("ME"));

	}
}